{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas数据排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series的排序：  \n",
    "***Series.sort_values(ascending=True, inplace=False)***\n",
    "参数说明：\n",
    "* ascending：默认为True升序排序，为False降序排序\n",
    "* inplace：是否修改原始Series\n",
    "\n",
    "DataFrame的排序：  \n",
    "***DataFrame.sort_values(by, ascending=True, inplace=False)***\n",
    "参数说明：\n",
    "* by：字符串或者List<字符串>，单列排序或者多列排序\n",
    "* ascending：bool或者List<bool>，升序还是降序，如果是list对应by的多列\n",
    "* inplace：是否修改原始DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 0、读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = \"./datas/beijing_tianqi/beijing_tianqi_2018.csv\"\n",
    "df = pd.read_csv(fpath)\n",
    "\n",
    "# 替换掉温度的后缀℃\n",
    "df.loc[:, \"bWendu\"] = df[\"bWendu\"].str.replace(\"℃\", \"\").astype('int32')\n",
    "df.loc[:, \"yWendu\"] = df[\"yWendu\"].str.replace(\"℃\", \"\").astype('int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0</td>\n",
       "      <td>-8</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2018-01-01       3      -6   晴~多云       东北风   1-2级   59       良         2\n",
       "1  2018-01-02       2      -5   阴~多云       东北风   1-2级   49       优         1\n",
       "2  2018-01-03       2      -5     多云        北风   1-2级   28       优         1\n",
       "3  2018-01-04       0      -8      阴       东北风   1-2级   28       优         1\n",
       "4  2018-01-05       3      -6   多云~晴       西北风   1-2级   50       优         1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、Series的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "271     21\n",
       "281     21\n",
       "249     22\n",
       "272     22\n",
       "301     22\n",
       "      ... \n",
       "317    266\n",
       "71     287\n",
       "91     287\n",
       "72     293\n",
       "86     387\n",
       "Name: aqi, Length: 365, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"aqi\"].sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "86     387\n",
       "72     293\n",
       "91     287\n",
       "71     287\n",
       "317    266\n",
       "      ... \n",
       "301     22\n",
       "272     22\n",
       "249     22\n",
       "281     21\n",
       "271     21\n",
       "Name: aqi, Length: 365, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"aqi\"].sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "225     中雨~小雨\n",
       "230     中雨~小雨\n",
       "197    中雨~雷阵雨\n",
       "196    中雨~雷阵雨\n",
       "112        多云\n",
       "        ...  \n",
       "191    雷阵雨~大雨\n",
       "219     雷阵雨~阴\n",
       "335      雾~多云\n",
       "353         霾\n",
       "348         霾\n",
       "Name: tianqi, Length: 365, dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"tianqi\"].sort_values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、DataFrame的排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1 单列排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>271</td>\n",
       "      <td>2018-09-29</td>\n",
       "      <td>22</td>\n",
       "      <td>11</td>\n",
       "      <td>晴</td>\n",
       "      <td>北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>21</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>281</td>\n",
       "      <td>2018-10-09</td>\n",
       "      <td>15</td>\n",
       "      <td>4</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>4-5级</td>\n",
       "      <td>21</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>249</td>\n",
       "      <td>2018-09-07</td>\n",
       "      <td>27</td>\n",
       "      <td>16</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>272</td>\n",
       "      <td>2018-09-30</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "      <td>多云</td>\n",
       "      <td>西北风</td>\n",
       "      <td>4-5级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>301</td>\n",
       "      <td>2018-10-29</td>\n",
       "      <td>15</td>\n",
       "      <td>3</td>\n",
       "      <td>晴</td>\n",
       "      <td>北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>317</td>\n",
       "      <td>2018-11-14</td>\n",
       "      <td>13</td>\n",
       "      <td>5</td>\n",
       "      <td>多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>266</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>71</td>\n",
       "      <td>2018-03-13</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>91</td>\n",
       "      <td>2018-04-02</td>\n",
       "      <td>26</td>\n",
       "      <td>11</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>72</td>\n",
       "      <td>2018-03-14</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>293</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>86</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>387</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "271  2018-09-29      22      11      晴        北风   3-4级   21       优         1\n",
       "281  2018-10-09      15       4   多云~晴       西北风   4-5级   21       优         1\n",
       "249  2018-09-07      27      16      晴       西北风   3-4级   22       优         1\n",
       "272  2018-09-30      19      13     多云       西北风   4-5级   22       优         1\n",
       "301  2018-10-29      15       3      晴        北风   3-4级   22       优         1\n",
       "..          ...     ...     ...    ...       ...    ...  ...     ...       ...\n",
       "317  2018-11-14      13       5     多云        南风   1-2级  266    重度污染         5\n",
       "71   2018-03-13      17       5   晴~多云        南风   1-2级  287    重度污染         5\n",
       "91   2018-04-02      26      11     多云        北风   1-2级  287    重度污染         5\n",
       "72   2018-03-14      15       6   多云~阴       东北风   1-2级  293    重度污染         5\n",
       "86   2018-03-28      25       9   多云~晴        东风   1-2级  387    严重污染         6\n",
       "\n",
       "[365 rows x 9 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by=\"aqi\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>86</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>387</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>72</td>\n",
       "      <td>2018-03-14</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>293</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>71</td>\n",
       "      <td>2018-03-13</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>91</td>\n",
       "      <td>2018-04-02</td>\n",
       "      <td>26</td>\n",
       "      <td>11</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>317</td>\n",
       "      <td>2018-11-14</td>\n",
       "      <td>13</td>\n",
       "      <td>5</td>\n",
       "      <td>多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>266</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>249</td>\n",
       "      <td>2018-09-07</td>\n",
       "      <td>27</td>\n",
       "      <td>16</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>301</td>\n",
       "      <td>2018-10-29</td>\n",
       "      <td>15</td>\n",
       "      <td>3</td>\n",
       "      <td>晴</td>\n",
       "      <td>北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>272</td>\n",
       "      <td>2018-09-30</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "      <td>多云</td>\n",
       "      <td>西北风</td>\n",
       "      <td>4-5级</td>\n",
       "      <td>22</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>271</td>\n",
       "      <td>2018-09-29</td>\n",
       "      <td>22</td>\n",
       "      <td>11</td>\n",
       "      <td>晴</td>\n",
       "      <td>北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>21</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>281</td>\n",
       "      <td>2018-10-09</td>\n",
       "      <td>15</td>\n",
       "      <td>4</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>4-5级</td>\n",
       "      <td>21</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "86   2018-03-28      25       9   多云~晴        东风   1-2级  387    严重污染         6\n",
       "72   2018-03-14      15       6   多云~阴       东北风   1-2级  293    重度污染         5\n",
       "71   2018-03-13      17       5   晴~多云        南风   1-2级  287    重度污染         5\n",
       "91   2018-04-02      26      11     多云        北风   1-2级  287    重度污染         5\n",
       "317  2018-11-14      13       5     多云        南风   1-2级  266    重度污染         5\n",
       "..          ...     ...     ...    ...       ...    ...  ...     ...       ...\n",
       "249  2018-09-07      27      16      晴       西北风   3-4级   22       优         1\n",
       "301  2018-10-29      15       3      晴        北风   3-4级   22       优         1\n",
       "272  2018-09-30      19      13     多云       西北风   4-5级   22       优         1\n",
       "271  2018-09-29      22      11      晴        北风   3-4级   21       优         1\n",
       "281  2018-10-09      15       4   多云~晴       西北风   4-5级   21       优         1\n",
       "\n",
       "[365 rows x 9 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by=\"aqi\", ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2 多列排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>360</td>\n",
       "      <td>2018-12-27</td>\n",
       "      <td>-5</td>\n",
       "      <td>-12</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>48</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>2018-01-23</td>\n",
       "      <td>-4</td>\n",
       "      <td>-12</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>31</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>2018-01-24</td>\n",
       "      <td>-4</td>\n",
       "      <td>-11</td>\n",
       "      <td>晴</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>34</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>340</td>\n",
       "      <td>2018-12-07</td>\n",
       "      <td>-4</td>\n",
       "      <td>-10</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>33</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>21</td>\n",
       "      <td>2018-01-22</td>\n",
       "      <td>-3</td>\n",
       "      <td>-10</td>\n",
       "      <td>小雪~多云</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>47</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>71</td>\n",
       "      <td>2018-03-13</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>90</td>\n",
       "      <td>2018-04-01</td>\n",
       "      <td>25</td>\n",
       "      <td>11</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>218</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>91</td>\n",
       "      <td>2018-04-02</td>\n",
       "      <td>26</td>\n",
       "      <td>11</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>85</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>243</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>86</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>387</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "360  2018-12-27      -5     -12   多云~晴       西北风     3级   48       优         1\n",
       "22   2018-01-23      -4     -12      晴       西北风   3-4级   31       优         1\n",
       "23   2018-01-24      -4     -11      晴       西南风   1-2级   34       优         1\n",
       "340  2018-12-07      -4     -10      晴       西北风     3级   33       优         1\n",
       "21   2018-01-22      -3     -10  小雪~多云        东风   1-2级   47       优         1\n",
       "..          ...     ...     ...    ...       ...    ...  ...     ...       ...\n",
       "71   2018-03-13      17       5   晴~多云        南风   1-2级  287    重度污染         5\n",
       "90   2018-04-01      25      11   晴~多云        南风   1-2级  218    重度污染         5\n",
       "91   2018-04-02      26      11     多云        北风   1-2级  287    重度污染         5\n",
       "85   2018-03-27      27      11      晴        南风   1-2级  243    重度污染         5\n",
       "86   2018-03-28      25       9   多云~晴        东风   1-2级  387    严重污染         6\n",
       "\n",
       "[365 rows x 9 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按空气质量等级、最高温度排序，默认升序\n",
    "df.sort_values(by=[\"aqiLevel\", \"bWendu\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>86</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>387</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>85</td>\n",
       "      <td>2018-03-27</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>243</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>91</td>\n",
       "      <td>2018-04-02</td>\n",
       "      <td>26</td>\n",
       "      <td>11</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>90</td>\n",
       "      <td>2018-04-01</td>\n",
       "      <td>25</td>\n",
       "      <td>11</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>218</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>71</td>\n",
       "      <td>2018-03-13</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>287</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>362</td>\n",
       "      <td>2018-12-29</td>\n",
       "      <td>-3</td>\n",
       "      <td>-12</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>29</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>2018-01-23</td>\n",
       "      <td>-4</td>\n",
       "      <td>-12</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3-4级</td>\n",
       "      <td>31</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>2018-01-24</td>\n",
       "      <td>-4</td>\n",
       "      <td>-11</td>\n",
       "      <td>晴</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>34</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>340</td>\n",
       "      <td>2018-12-07</td>\n",
       "      <td>-4</td>\n",
       "      <td>-10</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>33</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>360</td>\n",
       "      <td>2018-12-27</td>\n",
       "      <td>-5</td>\n",
       "      <td>-12</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>3级</td>\n",
       "      <td>48</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "86   2018-03-28      25       9   多云~晴        东风   1-2级  387    严重污染         6\n",
       "85   2018-03-27      27      11      晴        南风   1-2级  243    重度污染         5\n",
       "91   2018-04-02      26      11     多云        北风   1-2级  287    重度污染         5\n",
       "90   2018-04-01      25      11   晴~多云        南风   1-2级  218    重度污染         5\n",
       "71   2018-03-13      17       5   晴~多云        南风   1-2级  287    重度污染         5\n",
       "..          ...     ...     ...    ...       ...    ...  ...     ...       ...\n",
       "362  2018-12-29      -3     -12      晴       西北风     2级   29       优         1\n",
       "22   2018-01-23      -4     -12      晴       西北风   3-4级   31       优         1\n",
       "23   2018-01-24      -4     -11      晴       西南风   1-2级   34       优         1\n",
       "340  2018-12-07      -4     -10      晴       西北风     3级   33       优         1\n",
       "360  2018-12-27      -5     -12   多云~晴       西北风     3级   48       优         1\n",
       "\n",
       "[365 rows x 9 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两个字段都是降序\n",
    "df.sort_values(by=[\"aqiLevel\", \"bWendu\"], ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>178</td>\n",
       "      <td>2018-06-28</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>33</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>149</td>\n",
       "      <td>2018-05-30</td>\n",
       "      <td>33</td>\n",
       "      <td>18</td>\n",
       "      <td>晴</td>\n",
       "      <td>西风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>46</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>206</td>\n",
       "      <td>2018-07-26</td>\n",
       "      <td>33</td>\n",
       "      <td>25</td>\n",
       "      <td>多云~雷阵雨</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>40</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>158</td>\n",
       "      <td>2018-06-08</td>\n",
       "      <td>32</td>\n",
       "      <td>19</td>\n",
       "      <td>多云~雷阵雨</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>43</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>205</td>\n",
       "      <td>2018-07-25</td>\n",
       "      <td>32</td>\n",
       "      <td>25</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>317</td>\n",
       "      <td>2018-11-14</td>\n",
       "      <td>13</td>\n",
       "      <td>5</td>\n",
       "      <td>多云</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>266</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>329</td>\n",
       "      <td>2018-11-26</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>多云</td>\n",
       "      <td>东南风</td>\n",
       "      <td>1级</td>\n",
       "      <td>245</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>335</td>\n",
       "      <td>2018-12-02</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>雾~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>234</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>57</td>\n",
       "      <td>2018-02-27</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>阴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>220</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>86</td>\n",
       "      <td>2018-03-28</td>\n",
       "      <td>25</td>\n",
       "      <td>9</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>东风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>387</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ymd  bWendu  yWendu  tianqi fengxiang fengli  aqi aqiInfo  \\\n",
       "178  2018-06-28      35      24    多云~晴        北风   1-2级   33       优   \n",
       "149  2018-05-30      33      18       晴        西风   1-2级   46       优   \n",
       "206  2018-07-26      33      25  多云~雷阵雨       东北风   1-2级   40       优   \n",
       "158  2018-06-08      32      19  多云~雷阵雨       西南风   1-2级   43       优   \n",
       "205  2018-07-25      32      25      多云        北风   1-2级   28       优   \n",
       "..          ...     ...     ...     ...       ...    ...  ...     ...   \n",
       "317  2018-11-14      13       5      多云        南风   1-2级  266    重度污染   \n",
       "329  2018-11-26      10       0      多云       东南风     1级  245    重度污染   \n",
       "335  2018-12-02       9       2    雾~多云       东北风     1级  234    重度污染   \n",
       "57   2018-02-27       7       0       阴        东风   1-2级  220    重度污染   \n",
       "86   2018-03-28      25       9    多云~晴        东风   1-2级  387    严重污染   \n",
       "\n",
       "     aqiLevel  \n",
       "178         1  \n",
       "149         1  \n",
       "206         1  \n",
       "158         1  \n",
       "205         1  \n",
       "..        ...  \n",
       "317         5  \n",
       "329         5  \n",
       "335         5  \n",
       "57          5  \n",
       "86          6  \n",
       "\n",
       "[365 rows x 9 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分别指定升序和降序\n",
    "df.sort_values(by=[\"aqiLevel\", \"bWendu\"], ascending=[True, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
